package com.ysyc.driver_ee.adapter;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.activity.DriverDetailsActivity;
import com.ysyc.driver_ee.activity.VerificationCodeActivity;
import com.ysyc.driver_ee.base.BaseAdapter;
import com.ysyc.driver_ee.model.DriverInfo;
import com.ysyc.driver_ee.tools.PermissionUtils;
import com.ysyc.driver_ee.tools.SpannableStringUtils;
import com.ysyc.driver_ee.tools.StatusSettingUtils;
import com.ysyc.driver_ee.widget.SureAndCancelDialog;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/11/3 13:56<br>
 * 版本： v1.0<br>
 */
public class CapacityOrderFromAdapter extends BaseAdapter<DriverInfo> {

    private SureAndCancelDialog sureAndCancelDialog;
    private DriverInfo items;

    public CapacityOrderFromAdapter(Activity activity, int layoutView) {
        super(activity, layoutView);
    }

    @Override
    public void convert(MyViewHolder holder, DriverInfo item) {
        View itemView = holder.itemView;
        items = item;
        ImageView iconView = itemView.findViewById(R.id.iv_icon_view);
        TextView nameView = itemView.findViewById(R.id.tv_name_view);
        TextView identityTv = itemView.findViewById(R.id.tv_identity_tv);
        TextView licensePlateView = itemView.findViewById(R.id.tv_license_plate_view);
        ImageView callPhoneView = itemView.findViewById(R.id.iv_call_phone_view);
        TextView locationView = itemView.findViewById(R.id.tv_location_view);
        TextView numberView = itemView.findViewById(R.id.tv_number_view);
        TextView statusView = itemView.findViewById(R.id.tv_status_view);
        StatusSettingUtils.setDriverAvatar(iconView, item.identity);
        nameView.setText(item.driverName);
        StatusSettingUtils.setIdentity(identityTv, item.identity);
        licensePlateView.setText(item.carFormCn + item.carNumber);
        locationView.setText("当前位置:" + item.driverMapName);
        callPhoneView.setOnClickListener(v -> {
            String[] permisson = {Manifest.permission.CALL_PHONE};
            PermissionUtils.requestPermissions(mActivity, 1, permisson, mPermissionGrant);
        });
        SpannableStringUtils.setUpTextStyle(numberView, "待完成订单:", item.pendingOrderNum + "", "#FFA97D36");
        StatusSettingUtils.setTransportStatus(statusView, item.transportStatus);
        itemView.setOnClickListener(v -> {
            Intent intent = new Intent(mActivity,DriverDetailsActivity.class);
            intent.putExtra("driverId",item.id);
            mActivity.startActivity(intent);

        });

    }

    public PermissionUtils.PermissionGrant mPermissionGrant = new PermissionUtils.PermissionGrant() {
        @Override
        public void onPermissionGranted(int requestCode) {
            switch (requestCode) {
                case 1:
                    if (sureAndCancelDialog == null) {
                        sureAndCancelDialog = new SureAndCancelDialog(mActivity);
                    }
                    sureAndCancelDialog.setTitleText("拨打"+items.driverName+"电话").setMessageText(items.telPhone).setSureText("拨打电话").setOnDialogClickListener(new SureAndCancelDialog.onDialogClickListener() {
                        @Override
                        public void onPositiveButtonClick(SureAndCancelDialog dialog) {
                            Intent intent = new Intent();
                            intent.setAction(Intent.ACTION_CALL);
                            intent.setData(Uri.parse("tel:" + items.telPhone));
                            //开启系统拨号器
                            mActivity.startActivity(intent);
                        }
                    });
                    sureAndCancelDialog.show();
                    break;
                default:
                    break;
            }
        }
    };


}
